/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plantObjects;

import control.Event;
import control.Eventhandler;
import java.util.ArrayList;

/**
 * Crane at the end of the system
 * it must handle the end storage and load the waggons(end of system)
 * @author guru
 */
public class CraneEnd implements TransformingUnit {

    private int time, timeToWaggon, changeTime;
    // position is true : crane in storage / false: crane next to waggon
    private boolean atStor;
    private boolean flagWaggon = true, flagStor = true;
    // conveyor belt before and after storage
    private TransformingUnit convToStor, convToWag;

    public CraneEnd(int time, int timeToWaggon, int changeTime, TransformingUnit convToStor, TransformingUnit convToWag) {
        this.time = time;
        this.timeToWaggon = timeToWaggon;
        this.changeTime = changeTime;
        atStor = true;
    }

    /**
     * Put a slap in end storage
     * @param Slap to put
     */
    public void put(Slap sl) {
        if (!atStor) {
            System.err.println("Loading without crane at endstorage ... \n\t... Prod Control sucks");
        }
        if (flagStor) {
            // simulate the movement from conv to stor and back to cony
            Eventhandler.add(new Event(Eventhandler.getTime() + time, convToStor));
            flagStor = false;
        } else {
            // slap load to stor
            flagStor = true;
            Slap.removeSlap(sl);
        }
    }

    public Slap get() {
        //TODO: get funktion schreiben -> warten auf mail antwort bzgl 2 actors

        return null;
    }

    public void orderSlap(ArrayList<Position> positions, String barType) {
        //TODO: funktion in abhängiogkeit vom anderen todo ...
    }

    /**
     * Load a slap to waggon and simulates the required time by a longer driving time .... not very cute, but it should work
     * @param Slap to
     */
    public void loadToWaggon(Slap sl) {
        if (flagWaggon) {
            Eventhandler.add(new Event(Eventhandler.getTime() + timeToWaggon, convToWag));
            //set flagWaggon on loading is taking place
            flagWaggon = false;
        } else {
            // loading finsihed, flagWaggon reset and remove slap
            flagWaggon = true;
            Slap.removeSlap(sl);
        }
    }

    /**
     * moves the crane to storage position if it isn't there
     * @return if it is in storage position = true /false = waggon position
     */
    public boolean moveToStor() {
        if (!atStor) {
            Eventhandler.add(new Event(Eventhandler.getTime() + changeTime, convToStor));
            atStor = true;
            return true;
        }
        return false;

    }

    /**
     * moves the crane to waggon position if it isn't there
     * @return if it is in waggon position = true /false = storage position
     */
    public boolean moveToWag() {
        if (atStor) {
            Eventhandler.add(new Event(Eventhandler.getTime() + changeTime, convToWag));
            atStor = false;
            return true;
        }
        return false;
    }

    /**
     * Checks if crane is idle
     * @return
     */
    public boolean isAvail() {
        //TODO isAvail Funktion CraneEnd
        return true;
    }
}
